package gui.panel;

import gui.Fenster;
import gui.Konstanten;
import gui.dialoge.LagerUmbenennenDialog;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

import steuerung.Controller;

/**
 * Das HierarchiePanel zeigt die Lagerhierarchie an
 * 
 * @author Wayne Fox
 */
public class HierarchiePanel extends JPanel implements Observer {
	private static final long serialVersionUID = 1L;

	private JTree trLager;
	private DefaultMutableTreeNode clickedLagerNode;
	private Fenster fenster;

	/**
	 * Legt ein HierarchiePanel an
	 * 
	 * @param f Ein Fenster-Objekt zum fokussieren des entsprechenden Tabs
	 */
	public HierarchiePanel(Fenster f) {
		fenster = f;

		initialisieren();
		Controller.getInstance().addModelObserver(this);
	}

	@Override
	public void update(Observable o, Object arg) {
		// TreeModel updaten
		TreeModel model = Controller.getInstance().getLagerModel();
		if (!model.equals(trLager.getModel())) {
			setTreeModel(model);
		}
	}

	private void initialisieren() {
		this.setPreferredSize(new Dimension(Konstanten.TABBEDPANE_LINKS_BREITE,
				Konstanten.TABBEDPANE_LINKS_HOEHE));
		this.setMaximumSize(new Dimension(Konstanten.TABBEDPANE_LINKS_BREITE,
				Konstanten.TABBEDPANE_LINKS_HOEHE));

		trLager = new JTree(new DefaultMutableTreeNode("Leer"));
		trLager.setCellRenderer(new DefaultTreeCellRenderer() {

			private static final long serialVersionUID = 1L;

			@Override
			public Color getBackgroundNonSelectionColor() {
				return UIManager.getColor("Panel.background");
			}

		});
		trLager.setBackground(UIManager.getColor("Panel.background"));

		JScrollPane spLager = new JScrollPane(trLager);
		spLager.setPreferredSize(new Dimension(
				Konstanten.TABBEDPANE_LINKS_BREITE - 18,
				Konstanten.TABBEDPANE_LINKS_HOEHE - 35));

		JPopupMenu popupMenu = new JPopupMenu();
		JMenuItem miUmbenennen = new JMenuItem(
				Konstanten.HIERARCHIE_POPUPITEM_LAGERUMBENENNEN);
		miUmbenennen.addActionListener(new PopupActionListener());
		popupMenu.add(miUmbenennen);

		trLager.addMouseListener(new PopupListener(popupMenu));

		this.add(spLager);
	}

	private void setTreeModel(TreeModel model) {
		trLager.setModel(model);
	}

	class PopupListener extends MouseAdapter {

		JPopupMenu popupMenu;

		public PopupListener(JPopupMenu menu) {
			popupMenu = menu;
		}

		@Override
		public void mousePressed(MouseEvent ev) {
			TreePath tp = trLager.getPathForLocation(ev.getX(), ev.getY());

			if (tp != null) {
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) tp
						.getLastPathComponent();
				Controller.getInstance().setAusgewaehlteNode(node);
				fenster.fokussiereLieferdetailsTab();
			}
		}

		@Override
		public void mouseReleased(MouseEvent ev) {
			if (SwingUtilities.isRightMouseButton(ev)) {
				showPopup(ev);
			}
		}

		private void showPopup(MouseEvent ev) {
			if (ev.isPopupTrigger()) {

				TreePath selPath = trLager.getPathForLocation(ev.getX(),
						ev.getY());

				if (selPath != null) {
					DefaultMutableTreeNode node = (DefaultMutableTreeNode) selPath
							.getLastPathComponent();

					if (!node.toString().equals(
							steuerung.Konstanten.LAGER_WURZEL_BEZEICHNUNG)) {
						popupMenu.show(ev.getComponent(), ev.getX(), ev.getY());
						clickedLagerNode = node;
					}
				} else {
					clickedLagerNode = null;
				}

			}
		}
	}

	class PopupActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent ev) {
			if (ev.getActionCommand().equals(
					Konstanten.HIERARCHIE_POPUPITEM_LAGERUMBENENNEN)) {
				if (clickedLagerNode != null) {
					new LagerUmbenennenDialog(fenster, clickedLagerNode);
				}
			}
		}
	}
}